from utils.gum_algorithm23.GUM import *
from utils.gum_algorithm23.parse_data import *
from utils.gum_algorithm23.preprocess_data import *


def evaluate_uncertainty(
        L2_filepath, filepath_dict, system_param, k=1, given_param="U"
):
    L2_filename = os.path.basename(L2_filepath)
    info = L2_filename.split("_")
    lid_code = info[1]
    if len(info) == 5:
        param = info[2]
    elif len(info) == 6:
        param = info[3]
    elif len(info) == 8:
        if "Fe" in info and "D" in info:
            param = "F"
        elif "Na" in info and "D" in info:
            param = "N"
        elif "W" in info and ("DW" in info or "DE" in info):
            param = "U"
        elif "W" in info and ("DN" in info or "DS" in info):
            param = "V"
        else:
            param = info[-3]
    data_L2 = parse_LD_data(L2_filepath)
    if lid_code == "LD4":
        if given_param == "D":
            data_L2 = data_L2[:, :2]
        elif given_param == "T":
            data_L2 = data_L2[:, (0, 2)]
        param = given_param
    data_L0_dict = get_cumulative_data(filepath_dict)
    data_L0 = None
    result_df = None
    # try:
    if param == "D":
        data_L0, result_df = get_GUM_D_uncertainty(
            data_L2,
            data_L0_dict,
            lid_code=lid_code,
            z0=system_param["z0"],
            dens0=system_param["dens0"],
            u_dens0=system_param["u_dens0"],
            tau=system_param["ray_tau"],
            u_tau=system_param["u_ray_tau"],
            alt_angle=system_param["alt_angle"],
            location_altitude=system_param["location_altitude"],
            height_resolution=system_param["height_resolution"],
            time_resolution=system_param["time_resolution"],
            pulse_repeat=system_param["ray_pulse_repeat"],
            k=k,
        )
    elif param == "F":
        data_L0, result_df = get_GUM_F_uncertainty(
            data_L2,
            data_L0_dict,
            lid_code=lid_code,
            z0=system_param["z0"],
            dens0=system_param["dens0"],
            u_dens0=system_param["u_dens0"],
            tau=system_param["res_tau"],
            u_tau=system_param["u_res_tau"],
            alt_angle=system_param["alt_angle"],
            location_altitude=system_param["location_altitude"],
            height_resolution=system_param["height_resolution"],
            time_resolution=system_param["time_resolution"],
            pulse_repeat=system_param["res_pulse_repeat"],
            k=k,
        )
    elif param == "N":
        data_L0, result_df = get_GUM_N_uncertainty(
            data_L2,
            data_L0_dict,
            lid_code=lid_code,
            z0=system_param["z0"],
            dens0=system_param["dens0"],
            u_dens0=system_param["u_dens0"],
            tau=system_param["res_tau"],
            u_tau=system_param["u_res_tau"],
            alt_angle=system_param["alt_angle"],
            location_altitude=system_param["location_altitude"],
            height_resolution=system_param["height_resolution"],
            time_resolution=system_param["time_resolution"],
            pulse_repeat=system_param["res_pulse_repeat"],
            k=k,
        )
    elif param == "T":
        data_L0, result_df = get_GUM_T_uncertainty(
            data_L2,
            data_L0_dict,
            lid_code=lid_code,
            z1=system_param["z1"],
            T1=system_param["T1"],
            u_T1=system_param["u_T1"],
            poly_coef=system_param["poly_coef"],
            c_T_RT=system_param["c_T_RT"],
            c_T_RV=system_param["c_T_RV"],
            table_T_resolution=system_param["table_T_resolution"],
            tau=system_param["ray_tau"],
            u_tau=system_param["u_ray_tau"],
            alt_angle=system_param["alt_angle"],
            location_altitude=system_param["location_altitude"],
            height_resolution=system_param["height_resolution"],
            time_resolution=system_param["time_resolution"],
            pulse_repeat=system_param["ray_pulse_repeat"],
            k=k,
        )
    elif param in ["W", "V", "U"]:
        data_L0, result_df = get_GUM_W_uncertainty(
            data_L2,
            data_L0_dict,
            lid_code=lid_code,
            cRay_V_RV=system_param["cRay_V_RV"],
            wavelen=system_param["res_wavelen"],
            u_wavelen=system_param["u_res_wavelen"],
            cRes_V_RT=system_param["cRes_V_RT"],
            cRes_V_RV=system_param["cRes_V_RV"],
            table_V_resolution=system_param["table_V_resolution"],
            tau=system_param["res_tau"],
            u_tau=system_param["u_res_tau"],
            alt_angle=system_param["alt_angle"],
            location_altitude=system_param["location_altitude"],
            height_resolution=system_param["height_resolution"],
            time_resolution=system_param["time_resolution"],
            pulse_repeat=system_param["res_pulse_repeat"],
            k=k,
            given_param=given_param,
        )
    # elif param == "P":
    #     data_L0, result_df = get_GUM_P_uncertainty(
    #         data_L2,
    #         data_L0_dict,
    #         lid_code=lid_code,
    #         z0=system_param["z0"],
    #         dens0=system_param["dens0"],
    #         u_dens0=system_param["u_dens0"],
    #         z1=system_param["z1"],
    #         T1=system_param["T1"],
    #         u_T1=system_param["u_T1"],
    #         tau=system_param["ray_tau"],
    #         u_tau=system_param["u_ray_tau"],
    #         alt_angle=system_param["alt_angle"],
    #         location_altitude=system_param["location_altitude"],
    #         height_resolution=system_param["height_resolution"],
    #         time_resolution=system_param["time_resolution"],
    #         pulse_repeat=system_param["ray_pulse_repeat"],
    #         k=k,
    #     )
    # except:
    #     print('File Error: '+L2_filepath)
    return data_L0, result_df


def get_GUM_D_uncertainty(
        data_L2,
        data_L0_dict,
        lid_code="LD1",
        z0=30,
        dens0=0.01841,
        u_dens0=0,
        tau=1e-9,
        u_tau=0,
        alt_angle=0,
        location_altitude=0,
        height_resolution=1,
        time_resolution=3600,
        pulse_repeat=30,
        k=1,
):
    # D
    # {'Ry_DV': []}
    # {'Ry_DV': []}
    # {'Ry_DN': [], 'Ry_DE': []}
    data_L0 = None
    result_df = None
    # if lid_code in ["LD1", "LD2"]:
    if "Ry_DV" in data_L0_dict:
        alt_angle = 90
        Ry_DV_data = data_L0_dict["Ry_DV"]
        data_L0 = np.hstack(
            (
                Ry_DV_data[:, 0].reshape(-1, 1),
                np.sum(Ry_DV_data[:, 1:], axis=1).reshape(-1, 1),
            )
        )
    # elif lid_code in ["LD3", "L3A", "L3B", "LD4"]:
    elif "Ry_DE" in data_L0_dict and "Ry_DN" in data_L0_dict:
        Ry_DE_data = data_L0_dict["Ry_DE"]
        Ry_DN_data = data_L0_dict["Ry_DN"]
        Ry_DE_mask = (20 <= Ry_DE_data[:, 0]) & (Ry_DE_data[:, 0] <= 80)
        Ry_DN_mask = (20 <= Ry_DN_data[:, 0]) & (Ry_DN_data[:, 0] <= 80)
        if np.sum(Ry_DE_data[Ry_DE_mask, 1:]) >= np.sum(Ry_DE_data[Ry_DN_mask, 1:]):
            data_L0 = Ry_DE_data[:, (0, 2)]
        else:
            data_L0 = Ry_DN_data[:, (0, 2)]
    elif "Ry_DE" in data_L0_dict:
        data_L0 = data_L0_dict["Ry_DE"][:, (0, 2)]
    elif "Ry_DN" in data_L0_dict:
        data_L0 = data_L0_dict["Ry_DN"][:, (0, 2)]
    if data_L0 is not None:
        mask = (20 <= data_L2[:, 0]) & (data_L2[:, 0] <= 80)
        data_L2 = data_L2[mask, :]
        result_df = GUM_RayD(
            data_L2,
            data_L0,
            param="D",
            z0=z0,
            dens0=dens0,
            u_dens0=u_dens0,
            tau=tau,
            u_tau=u_tau,
            alt_angle=alt_angle,
            location_altitude=location_altitude,
            height_resolution=height_resolution,
            time_resolution=time_resolution,
            pulse_repeat=pulse_repeat,
            k=k,
        )
    return data_L0, result_df


def get_GUM_F_uncertainty(
        data_L2,
        data_L0_dict,
        lid_code="LD1",
        z0=30,
        dens0=0.01841,
        u_dens0=0,
        tau=1e-9,
        u_tau=0,
        alt_angle=0,
        location_altitude=0,
        height_resolution=1,
        time_resolution=3600,
        pulse_repeat=30,
        k=1,
):
    # Fe
    # {'Fe_DN': [], 'Fe_DE': []}
    data_L0 = None
    result_df = None
    # if lid_code in ["LD1"]:
    if "Fe_DE" in data_L0_dict and "Fe_DN" in data_L0_dict:
        Fe_DE_data = data_L0_dict["Fe_DE"]
        Fe_DN_data = data_L0_dict["Fe_DN"]
        Fe_DE_mask = (80 <= Fe_DE_data[:, 0]) & (Fe_DE_data[:, 0] <= 100)
        Fe_DN_mask = (80 <= Fe_DN_data[:, 0]) & (Fe_DN_data[:, 0] <= 100)
        if np.sum(Fe_DE_data[Fe_DE_mask, 1:]) >= np.sum(Fe_DN_data[Fe_DN_mask, 1:]):
            data_L0 = np.hstack(
                (
                    Fe_DE_data[:, 0].reshape(-1, 1),
                    np.sum(Fe_DE_data[:, 1:], axis=1).reshape(-1, 1),
                )
            )
        else:
            data_L0 = np.hstack(
                (
                    Fe_DN_data[:, 0].reshape(-1, 1),
                    np.sum(Fe_DN_data[:, 1:], axis=1).reshape(-1, 1),
                )
            )
    elif "Fe_DE" in data_L0_dict:
        Fe_DE_data = data_L0_dict["Fe_DE"]
        data_L0 = np.hstack(
            (
                Fe_DE_data[:, 0].reshape(-1, 1),
                np.sum(Fe_DE_data[:, 1:], axis=1).reshape(-1, 1),
            )
        )
    elif "Fe_DN" in data_L0_dict:
        Fe_DN_data = data_L0_dict["Fe_DN"]
        data_L0 = np.hstack(
            (
                Fe_DN_data[:, 0].reshape(-1, 1),
                np.sum(Fe_DN_data[:, 1:], axis=1).reshape(-1, 1),
            )
        )
    if data_L0 is not None:
        result_df = GUM_RayD(
            data_L2,
            data_L0,
            param="F",
            z0=z0,
            dens0=dens0,
            u_dens0=u_dens0,
            tau=tau,
            u_tau=u_tau,
            alt_angle=alt_angle,
            location_altitude=location_altitude,
            height_resolution=height_resolution,
            time_resolution=time_resolution,
            pulse_repeat=pulse_repeat,
            k=k,
        )
    return data_L0, result_df


def get_GUM_N_uncertainty(
        data_L2,
        data_L0_dict,
        lid_code="LD1",
        z0=30,
        dens0=0.01841,
        u_dens0=0,
        tau=1e-9,
        u_tau=0,
        alt_angle=0,
        location_altitude=0,
        height_resolution=1,
        time_resolution=3600,
        pulse_repeat=30,
        k=1,
):
    # Na
    # {'Na_DV': []}
    # {'Na_DN': [], 'Na_DE': []}
    data_L0 = None
    result_df = None
    # if lid_code in ["LD2"]:
    if "Na_DV" in data_L0_dict:
        alt_angle = 90
        Na_DV_L0 = data_L0_dict["Na_DV"]
        data_L0 = np.hstack(
            (
                Na_DV_L0[:, 0].reshape(-1, 1),
                np.sum(Na_DV_L0[:, 1:], axis=1).reshape(-1, 1),
            )
        )
    # elif lid_code in ["LD3", "L3A", "L3B"]:
    elif "Na_DE" in data_L0_dict and "Na_DN" in data_L0_dict:
        Na_DE_data = data_L0_dict["Na_DE"]
        Na_DN_data = data_L0_dict["Na_DN"]
        Na_DE_mask = (80 <= Na_DE_data[:, 0]) & (Na_DE_data[:, 0] <= 100)
        Na_DN_mask = (80 <= Na_DN_data[:, 0]) & (Na_DN_data[:, 0] <= 100)
        if np.sum(Na_DE_data[Na_DE_mask, 1:]) >= np.sum(Na_DN_data[Na_DN_mask, 1:]):
            data_L0 = np.hstack(
                (
                    Na_DE_data[:, 0].reshape(-1, 1),
                    np.sum(Na_DE_data[:, 1:], axis=1).reshape(-1, 1),
                )
            )
        else:
            data_L0 = np.hstack(
                (
                    Na_DN_data[:, 0].reshape(-1, 1),
                    np.sum(Na_DN_data[:, 1:], axis=1).reshape(-1, 1),
                )
            )
    elif "Na_DE" in data_L0_dict:
        Na_DE_data = data_L0_dict["Na_DE"]
        data_L0 = np.hstack(
            (
                Na_DE_data[:, 0].reshape(-1, 1),
                np.sum(Na_DE_data[:, 1:], axis=1).reshape(-1, 1),
            )
        )
    elif "Na_DN" in data_L0_dict:
        Na_DN_data = data_L0_dict["Na_DN"]
        data_L0 = np.hstack(
            (
                Na_DN_data[:, 0].reshape(-1, 1),
                np.sum(Na_DN_data[:, 1:], axis=1).reshape(-1, 1),
            )
        )
    if data_L0 is not None:
        result_df = GUM_RayD(
            data_L2,
            data_L0,
            param="N",
            z0=z0,
            dens0=dens0,
            u_dens0=u_dens0,
            tau=tau,
            u_tau=u_tau,
            alt_angle=alt_angle,
            location_altitude=location_altitude,
            height_resolution=height_resolution,
            time_resolution=time_resolution,
            pulse_repeat=pulse_repeat,
            k=k,
        )
    return data_L0, result_df


def get_GUM_T_uncertainty(
        data_L2,
        data_L0_dict,
        lid_code="LD1",
        z0=30,
        dens0=0.01841,
        u_dens0=0,
        z1=80,
        T1=209.6083,
        u_T1=0,
        poly_coef=None,
        c_T_RT=0.00349,
        c_T_RV=0.00349,
        table_T_resolution=0.1,
        tau=1e-9,
        u_tau=0,
        alt_angle=0,
        location_altitude=0,
        height_resolution=1,
        time_resolution=3600,
        pulse_repeat=30,
        k=1,
):
    # T
    # {'Ry_DV': [], 'Rm_DV': [], 'Fe_DN': [], 'Fe_DE': []}
    # {'Ry_DV': [], 'Rm_DV': [], 'Na_DV': []}
    # {'Ry_DN': [], 'Ry_DE': [], 'Na_DN': [], 'Na_DE': []}
    ray_data_L0 = None
    ray_result_df = None
    # if lid_code in ["LD1", "LD2"]:
    if "Ry_DV" in data_L0_dict:
        alt_angle = 90
        Ry_DV_data = data_L0_dict["Ry_DV"]
        ray_data_L0 = np.hstack(
            (
                Ry_DV_data[:, 0].reshape(-1, 1),
                np.sum(Ry_DV_data[:, 1:], axis=1).reshape(-1, 1),
            )
        )
    # elif lid_code in ["LD3", "L3A", "L3B", "LD4"]:
    elif "Ry_DE" in data_L0_dict and "Ry_DN" in data_L0_dict:
        Ry_DE_data = data_L0_dict["Ry_DE"]
        Ry_DN_data = data_L0_dict["Ry_DN"]
        Ry_DE_mask = (20 <= Ry_DE_data[:, 0]) & (Ry_DE_data[:, 0] <= 80)
        Ry_DN_mask = (20 <= Ry_DN_data[:, 0]) & (Ry_DN_data[:, 0] <= 80)
        if np.sum(Ry_DE_data[Ry_DE_mask, 1:]) >= np.sum(Ry_DE_data[Ry_DN_mask, 1:]):
            ray_data_L0 = Ry_DE_data[:, (0, 2)]
        else:
            ray_data_L0 = Ry_DN_data[:, (0, 2)]
    elif "Ry_DE" in data_L0_dict:
        ray_data_L0 = data_L0_dict["Ry_DE"][:, (0, 2)]
    elif "Ry_DN" in data_L0_dict:
        ray_data_L0 = data_L0_dict["Ry_DN"][:, (0, 2)]
    if ray_data_L0 is not None:
        ray_mask = (20 <= data_L2[:, 0]) & (data_L2[:, 0] <= 80)
        ray_data_L2 = data_L2[ray_mask, :]
        if ray_data_L2.size != 0:
            ray_result_df = GUM_RayT(
                ray_data_L2,
                ray_data_L0,
                z0=z0,
                dens0=dens0,
                u_dens0=u_dens0,
                z1=z1,
                T1=T1,
                u_T1=u_T1,
                tau=tau,
                u_tau=u_tau,
                alt_angle=alt_angle,
                location_altitude=location_altitude,
                height_resolution=height_resolution,
                time_resolution=time_resolution,
                pulse_repeat=pulse_repeat,
                k=k,
            )
    res_data_L0 = None
    res_result_df = None
    # if lid_code in ["LD1"]:
    mode_Res = 0
    if "Na_DV" in data_L0_dict:
        alt_angle = 90
        res_data_L0 = data_L0_dict["Na_DV"][:, (0, 1, 3, 5)]
    elif "Fe_DE" in data_L0_dict and "Fe_DN" in data_L0_dict:
        Fe_DE_data = data_L0_dict["Fe_DE"]
        Fe_DN_data = data_L0_dict["Fe_DN"]
        Fe_DE_mask = (80 <= Fe_DE_data[:, 0]) & (Fe_DE_data[:, 0] <= 100)
        Fe_DN_mask = (80 <= Fe_DN_data[:, 0]) & (Fe_DN_data[:, 0] <= 100)
        if np.sum(Fe_DE_data[Fe_DE_mask, 1:]) >= np.sum(Fe_DN_data[Fe_DN_mask, 1:]):
            res_data_L0 = Fe_DE_data[:, (0, 3, 1, 2)]
        else:
            res_data_L0 = Fe_DN_data[:, (0, 3, 1, 2)]
    elif "Fe_DE" in data_L0_dict:
        res_data_L0 = data_L0_dict["Fe_DE"][:, (0, 3, 1, 2)]
    elif "Fe_DN" in data_L0_dict:
        res_data_L0 = data_L0_dict["Fe_DN"][:, (0, 3, 1, 2)]
    # elif lid_code in ["LD3", "L3A", "L3B"]:
    #     mode_Res = 0
    elif "Na_DE" in data_L0_dict and "Na_DN" in data_L0_dict:
        Na_DE_data = data_L0_dict["Na_DE"]
        Na_DN_data = data_L0_dict["Na_DN"]
        Na_DE_mask = (80 <= Na_DE_data[:, 0]) & (Na_DE_data[:, 0] <= 100)
        Na_DN_mask = (80 <= Na_DN_data[:, 0]) & (Na_DN_data[:, 0] <= 100)
        if np.sum(Na_DE_data[Na_DE_mask, 1:]) >= np.sum(Na_DN_data[Na_DN_mask, 1:]):
            res_data_L0 = Na_DE_data[:, (0, 1, 3, 5)]
        else:
            res_data_L0 = Na_DN_data[:, (0, 1, 3, 5)]
    elif "Na_DE" in data_L0_dict:
        res_data_L0 = data_L0_dict["Na_DE"][:, (0, 1, 3, 5)]
    elif "Na_DN" in data_L0_dict:
        res_data_L0 = data_L0_dict["Na_DN"][:, (0, 1, 3, 5)]
    elif lid_code in ["LD2"]:
        mode_Res = 0
    if res_data_L0 is not None:
        res_mask = data_L2[:, 0] > 80
        res_data_L2 = data_L2[res_mask, :]
        if res_data_L2.size != 0:
            res_result_df = GUM_ResT(
                res_data_L2,
                res_data_L0,
                mode=mode_Res,
                c_T_RT=c_T_RT,
                c_T_RV=c_T_RV,
                table_T_resolution=table_T_resolution,
                tau=tau,
                u_tau=u_tau,
                alt_angle=alt_angle,
                location_altitude=location_altitude,
                height_resolution=height_resolution,
                time_resolution=time_resolution,
                pulse_repeat=pulse_repeat,
                k=k,
            )
    header1 = [
        "高度(km)",
        "温度(K)",
        "U(K)_GUM扩展不确定度",
        "u(K)_标准不确定度",
        "u1(K)_探测点光子噪声",
        "u2(K)_参考点光子噪声",
        "u3(K)_背景噪声",
        "u4(K)_探测点渡越时间",
        "u5(K)_参考点渡越时间",
        "u6(K)_参考温度",
    ]
    header2 = [
        "u7(K)_f0通道光子噪声",
        "u8(K)_f+通道光子噪声",
        "u9(K)_f-通道光子噪声",
        "u10(K)_f0通道背景噪声",
        "u11(K)_f+通道背景噪声",
        "u12(K)_f-通道背景噪声",
        "u13(K)_f0通道渡越时间",
        "u14(K)_f+通道渡越时间",
        "u15(K)_f-通道渡越时间",
        "u16(K)_温风查算表温度分辨率",
    ]
    header3 = [
        "u17(K)_拉曼体制高阶通道光子噪声",
        "u18(K)_拉曼体制低阶通道光子噪声",
        "u19(K)_拉曼体制高阶通道背景噪声",
        "u20(K)_拉曼体制低阶通道背景噪声",
        "u21(K)_拉曼体制高阶通道渡越时间",
        "u22(K)_拉曼体制低阶通道渡越时间",
    ]
    header = header1 + header2 + header3
    result = pd.DataFrame(columns=header)
    if ray_result_df is None:
        result_df = pd.concat([result, res_result_df])
        data_L0 = res_data_L0
    elif res_result_df is None:
        result_df = pd.concat([result, ray_result_df])
        data_L0 = ray_data_L0
    else:
        result_df = pd.concat([result, ray_result_df, res_result_df])
        result_df = result_df.reset_index(drop=True)
        data_L0 = ray_data_L0
    result_df.dropna(axis=1, how="all", inplace=True)
    return data_L0, result_df


def get_GUM_W_uncertainty(
        data_L2,
        data_L0_dict,
        lid_code="LD1",
        cRay_V_RV=None,
        wavelen=532,
        u_wavelen=0.01,
        cRes_V_RT=0.00349,
        cRes_V_RV=0.00349,
        table_V_resolution=0.1,
        tau=1e-9,
        u_tau=0,
        alt_angle=0,
        location_altitude=0,
        height_resolution=1,
        time_resolution=3600,
        pulse_repeat=30,
        k=1,
        given_param="U",
):
    # W
    # {'Ry_DE': [], 'Fe_DE': [],'Ry_DN': [], 'Fe_DN': []}
    # {'Ry_DE': [], 'Na_DE': [],'Ry_DN': [], 'Na_DN': []}
    # {'Ry_DE': [], 'Na_DE': [],'Ry_DN': [], 'Na_DN': []}
    ray_data_L0 = None
    ray_result_df = None
    if lid_code in ["LD1"]:
        mode_Ray = 0
    else:
        mode_Ray = 1
    # if lid_code in ["LD1", "LD2", "LD3", "L3A", "L3B", "LD4"]:
    if given_param == "U" and "Ry_DE" in data_L0_dict:
        Ry_DE_data = data_L0_dict["Ry_DE"]
        ray_data_L0 = Ry_DE_data[:, :3]
    elif given_param == "V" and "Ry_DN" in data_L0_dict:
        Ry_DN_data = data_L0_dict["Ry_DN"]
        ray_data_L0 = Ry_DN_data[:, :3]
    if ray_data_L0 is not None:
        Ray_mask = (20 <= data_L2[:, 0]) & (data_L2[:, 0] <= 80)
        ray_data_L2 = data_L2[Ray_mask, :]
        if ray_data_L2.size != 0:
            ray_result_df = GUM_RayW(
                ray_data_L2,
                ray_data_L0,
                mode=mode_Ray,
                c_V_RV=cRay_V_RV,
                wavelen=wavelen,
                u_wavelen=u_wavelen,
                tau=tau,
                u_tau=u_tau,
                alt_angle=alt_angle,
                location_altitude=location_altitude,
                height_resolution=height_resolution,
                time_resolution=time_resolution,
                pulse_repeat=pulse_repeat,
                k=1,
            )
    res_data_L0 = None
    res_result_df = None
    # if lid_code in ["LD1"]:
    mode_Res = 0
    if given_param == "U" and "Fe_DE" in data_L0_dict:
        res_data_L0 = data_L0_dict["Fe_DE"][:, (0, 3, 1, 2)]
    elif given_param == "V" and "Fe_DN" in data_L0_dict:
        res_data_L0 = data_L0_dict["Fe_DN"][:, (0, 3, 1, 2)]
    # elif lid_code in ["LD2", "LD3", "L3A", "L3B"]:
    # mode_Res = 0
    elif given_param == "U" and "Na_DE" in data_L0_dict:
        res_data_L0 = data_L0_dict["Na_DE"][:, (0, 1, 3, 5)]
    elif given_param == "V" and "Na_DN" in data_L0_dict:
        res_data_L0 = data_L0_dict["Na_DN"][:, (0, 1, 3, 5)]
    if res_data_L0 is not None:
        res_mask = data_L2[:, 0] > 80
        res_data_L2 = data_L2[res_mask, :]
        if res_data_L2.size != 0:
            res_result_df = GUM_ResW(
                res_data_L2,
                res_data_L0,
                mode=mode_Res,
                cRes_V_RT=cRes_V_RT,
                cRes_V_RV=cRes_V_RV,
                table_V_resolution=table_V_resolution,
                tau=tau,
                u_tau=u_tau,
                alt_angle=alt_angle,
                location_altitude=location_altitude,
                height_resolution=height_resolution,
                time_resolution=time_resolution,
                pulse_repeat=pulse_repeat,
                k=k,
            )
    header1 = [
        "高度(km)",
        "风速(m/s)",
        "U(m/s)_GUM扩展不确定度",
        "u(m/s)_标准不确定度",
        "u1(m/s)_瑞利探测通道光子噪声",
        "u2(m/s)_瑞利参考通道光子噪声",
        "u3(m/s)_瑞利探测通道背景噪声",
        "u4(m/s)_瑞利参考通道背景噪声",
        "u5(m/s)_瑞利探测通道渡越时间",
        "u6(m/s)_瑞利参考通道渡越时间",
        "u7(m/s)_瑞利测风锁频波长",
    ]
    header2 = [
        "u8(m/s)_f0通道光子噪声",
        "u9(m/s)_f+通道光子噪声",
        "u10(m/s)_f-通道光子噪声",
        "u11(m/s)_f0通道背景噪声",
        "u12(m/s)_f+通道背景噪声",
        "u13(m/s)_f-通道背景噪声",
        "u14(m/s)_f0通道渡越时间",
        "u15(m/s)_f+通道渡越时间",
        "u16(m/s)_f-通道渡越时间",
        "u17(m/s)_温风查算表风速分辨率",
    ]
    header = header1 + header2
    result = pd.DataFrame(columns=header)
    if ray_result_df is None:
        result_df = pd.concat([result, res_result_df])
        data_L0 = res_data_L0
    elif res_result_df is None:
        result_df = pd.concat([result, ray_result_df])
        data_L0 = ray_data_L0
    else:
        result_df = pd.concat([result, ray_result_df, res_result_df])
        result_df = result_df.reset_index(drop=True)
        data_L0 = ray_data_L0
    result_df.dropna(axis=1, how="all", inplace=True)
    return data_L0, result_df


if __name__ == "__main__":
    L2_filepath = r"D:\1.Work\1.Data\新型地基\数据\实测数据\20230920\2023年09月20日数据分享\L2\Density\Atmosphere\AL_LD2_D_20230829140006_L2.dat"
    L0_path = (
        r"D:\1.Work\1.Data\新型地基\数据\实测数据\20230920\2023年09月20日数据分享\L0"
    )
